﻿// =================================================================== 
// 项目说明
//====================================================================
// sushee @Copy Right 2019
// 文件： JobRecordRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/9/19
// 负责人：sushee
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using Dapper;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///作业记录 仓储层
    /// </summary>	
    public class JobRecordRepository : BaseRepository<JobRecord>
    {

        #region 构造

        public JobRecordRepository() : base()
        {
        }

        public JobRecordRepository(string connectionString) : base(connectionString)
        {
        }

        public JobRecordRepository(DatabaseContext dbContext) : base(dbContext)
        {
        }

        #endregion

        public JobRecord GetLastExcuteJobRecordInfo(JobEnum jobEnum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT top 1 * FROM [JobRecord] WHERE JobId=" + (int)jobEnum);
            strSql.Append(" ORDER BY CreatedDate DESC");
            return Database.Connection.QueryFirstOrDefault<JobRecord>(strSql.ToString());

        }

        public int GetErrorCount(int platform, DateTime dt)
        {
            string szSql = "SELECT COUNT(1) FROM [ErrorLog] WHERE Platform=@Platform AND CreatedDate>@CreatedDate";
            return Database.Connection.ExecuteScalar<int>(szSql, new { Platform = platform, CreatedDate = dt });
        }

        /// <summary>
        /// 更新时间\成功数量
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="jobID"></param>
        /// <param name="successCount"></param>
        public void UpdateLastDataRecordTime(int categoryID,int jobID,int successCount)
        {
            string szSql = "UPDATE [JobRecord] SET LastDataRecordTime=GETDATE(),SuccessCount=@SuccessCount WHERE DataCategoryID=@DataCategoryID AND JobId=@JobId AND SuccessCount=0 AND LastDataRecordTime is null";
            Database.Connection.Execute(szSql, new { DataCategoryID = categoryID, JobId = jobID, SuccessCount=successCount });
        }
    }
}